package com.txw.javaCore.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.txw.javaCore.model.entity.AppInfo;
import com.txw.javaCore.model.mapper.AppInfoMapper;
import com.txw.javaCore.model.mapper.DataDictMapper;
import com.txw.javaCore.model.request.AppSearchRequest;
import com.txw.javaCore.server.common.SystemDataConfig;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION:
 * @Author: 涂玄武
 * @DATE: 2020/12/17 16:52
 */
@Service
public class AppService {

    private static final Logger log = LoggerFactory.getLogger(AppService.class);

    @Autowired
    private AppInfoMapper appInfoMapper;

    @Autowired
    private DataDictMapper dataDictMapper;

    /**
     * 分页列表模糊搜索
     * @param request
     * @return
     * @throws Exception
     */
    public PageInfo search(AppSearchRequest request) throws Exception{
        PageHelper.startPage(request.getPageNo(),request.getPageSize());
        return new PageInfo<>(appInfoMapper.selectAll(request));
    }

    /**
     * 详情
     * @param id
     * @return
     * @throws Exception
     */
    public AppInfo getInfo(final Integer id) throws Exception{
        //防止空指针新写法：Optional.ofNullable()
        Optional<AppInfo> optional = Optional.ofNullable(appInfoMapper.selectByPrimaryKey(id));
        if (optional.isPresent()){
            AppInfo appInfo = optional.get();
            if (StringUtils.isNoneBlank(appInfo.getDevice())){
                appInfo.setDeviceTypes(Arrays.asList(StringUtils.split(appInfo.getDevice(),",")));
            }
            return appInfo;
        }
        return null;
    }

    /**
     * 添加
     * @param appInfo
     * @return
     * @throws Exception
     */
    public int add(AppInfo appInfo) throws Exception{
        appInfo.setCreateTime(DateTime.now().toDate());
        appInfo.setId(null);
        appInfoMapper.insertSelective(appInfo);
        return appInfo.getId();
    }

    /**
     * 获取app应用管理所需要的的字典信息
     */
    public Map<String,Object> getAppDataDict(){
        Map<String,Object> resMap = Maps.newHashMap();

        //方式一:存在硬编码的问题，若数据库中type_code改变，则引用到的所有程序均要改变
//        resMap.put("osType",dataDictMapper.selectByType("os_type"));
//        resMap.put("deviceType",dataDictMapper.selectByType("device_type"));

        //方式一进阶：使用枚举类存储os_type/device_type
//        resMap.put("osType",dataDictMapper.selectByType(DataDictEnum.AppOsType.getCode()));
//        resMap.put("deviceType",dataDictMapper.selectByType(DataDictEnum.AppDeviceType.getCode()));
//        return resMap;
        
        //方式二：减少请求次数即减少数据库的查询（使用本地缓存存储数据字典）
        resMap.put("osType", SystemDataConfig.getAppDataDictOs());
        resMap.put("deviceType",SystemDataConfig.getAppDataDictDevice());
        return resMap;

    }

    /**
     * 分页列表模糊搜索V2
     * @param request
     * @return
     * @throws Exception
     */
    public PageInfo searchV2(AppSearchRequest request) throws Exception{
        //        //第一种写法，遍历转换
//        if (pageInfo.getList() != null && !pageInfo.getList().isEmpty()){
//            List<AppInfo> list = pageInfo.getList();
//            list.forEach(new Consumer<AppInfo>() {
//                @Override
//                public void accept(AppInfo appInfo) {
//                    //抛弃
//                }
//            });
//        }

        //第二种写法
        PageHelper.startPage(request.getPageNo(),request.getPageSize());
        return new PageInfo<>(appInfoMapper.selectAllV2(request));
    }


}
